package com.hippo.core.ui.compose.register

import LoadingDialog
import android.R.attr.duration
import android.R.attr.onClick
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter.Companion.tint
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.ui.components.InputEdit
import com.hippo.core.ui.components.PasswordEdit
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.theme.ColorHint
import com.hippo.core.ui.theme.ColorBackgroundPrimary
import com.hippo.core.ui.theme.ColorPrimary
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.ui.theme.PaddingMedium
import com.hippo.core.utils.UiHandler
import com.hippo.core.vm.RegisterVM

/**
 * 注册
 */


@Composable
fun RegisterScreen(onBack: () -> Unit,onMain: () -> Unit,vm: RegisterVM = viewModel()) {

    val uiState = vm.uiState

    var showRegionDialog by remember { mutableStateOf(false) }

    val context = LocalContext.current


    if(uiState.isLoading){
        LoadingDialog{
            vm.dismissLoading()
        }
    }


    LaunchedEffect(uiState) {
        if (uiState.registerSuccess) {
            UiHandler.showToast(R.string.register_11)
            onMain()
        }
    }

    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(color = ColorBackgroundPrimary)
            .padding(PaddingMedium)
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
        ) {

            VerticalSpacer(20.dp, modifier = Modifier.weight(1F))

            Image(
                modifier = Modifier
                    .size(100.dp)
                    .align(Alignment.CenterHorizontally),
                painter = painterResource(id = R.mipmap.login_logo),
                contentDescription = "logo",
            )

            VerticalSpacer(20.dp)

            InputEdit(
                text = uiState.userEmail.orEmpty(),
                hint = stringResource(R.string.register_1),
                isError = uiState.userEmailError,
                onValueChange = {
                    vm.updateState(uiState.copy(userEmail = it, userEmailError = false))
                },
                leftPainter = painterResource(R.mipmap.mail),
                keyboardOptions = KeyboardOptions(
                    keyboardType = KeyboardType.Email
                )
            )

            VerticalSpacer(10.dp)

            Box {
                InputEdit(
                    isError = uiState.verifyCodeError,
                    text = uiState.verifyCode.orEmpty(),
                    hint = stringResource(R.string.verify_5),
                    onValueChange = {
                        vm.updateState(uiState.copy(verifyCode = it, verifyCodeError = false))
                    },
                    leftPainter = painterResource(R.mipmap.msg_ok),
                    keyboardOptions = KeyboardOptions(
                        keyboardType = KeyboardType.Number
                    )
                )

                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .align(Alignment.CenterEnd)
                        .padding(end = 20.dp)
                ) {
                    Image(
                        modifier = Modifier
                            .size(25.dp),
                        imageVector = Icons.Filled.MoreVert,
                        contentDescription = "send",
                        colorFilter = tint(ColorHint)
                    )

                    TextButton (
                        enabled = uiState.countDown <= 0,
                        onClick = {
                            vm.sendEmailCode()
                        }
                    ){

                        val msg = if(uiState.countDown > 0){
                            stringResource(R.string.register_10) + "( ${uiState.countDown})"
                        }else{
                            stringResource(R.string.register_10)
                        }
                        Text(
                            text = msg,
                            color = if(uiState.countDown <= 0) MaterialTheme.colorScheme.primary else Color.Gray,
                        )
                    }
                }

            }


            VerticalSpacer(10.dp)

            PasswordEdit(
                isError = uiState.passwordError,
                text = uiState.password.orEmpty(),
                hint = stringResource(R.string.register_4),
                onValueChange = {
                    vm.updateState(uiState.copy(password = it))
                },
                leftPainter = painterResource(R.mipmap.pwd),
            )

            VerticalSpacer(10.dp)

            PasswordEdit(
                isError = uiState.confirmPasswordError,
                text = uiState.confirmPassword.orEmpty(),
                hint = stringResource(R.string.register_5),
                onValueChange = {
                    vm.updateState(uiState.copy(confirmPassword  = it))
                },
                leftPainter = painterResource(R.mipmap.pwd),
            )

            VerticalSpacer(10.dp)

            InputEdit(
                text = "${uiState.englishName.orEmpty()}(+${uiState.countryCode.orEmpty()})",
                enable = false,
                hint = "",
                onValueChange = {
                },
                leftPainter = painterResource(R.mipmap.location),
                rightPainter = painterResource(R.mipmap.down),
                onRightClick = {
                    vm.getCountryList {
                        showRegionDialog = true
                    }
                }
            )

            VerticalSpacer(30.dp)



            ThemeButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.register_6),
                onClick = {
                    vm.register(context)
                }
            )

            VerticalSpacer(20.dp)

            Text(
                text = stringResource(R.string.register_7),
                color = ColorPrimary,
                textDecoration = TextDecoration.Underline,
                modifier = Modifier
                    .align(Alignment.CenterHorizontally)
                    .click {
                        onBack()
                    }
            )

            VerticalSpacer(20.dp, modifier = Modifier.weight(3F))
        }

    }

    if(showRegionDialog){
        uiState.countryList?.let {
            RegionBottomSheet(
                countryList = it,
                selectedIndex = 0,
                onItemSelected = { country ->
                    showRegionDialog = false
                    vm.updateState(uiState.copy(
                        countryId  = country.countryId,
                        countryCode  = country.countryCode,
                        englishName  = country.englishName,
                        ))
                },
                onDismissRequest = {
                    showRegionDialog = false
                }
            )
        }

    }



}


@Preview
@Composable
private fun RegisterContentPreview() {
    HippoTheme {
        RegisterScreen(
            onBack = {},
            onMain = {}
        )
    }
}







